﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChiTietBCCongNo_DAO
    {
        private static ChiTietBCCongNo_DAO m_instance = null;

        public static ChiTietBCCongNo_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new ChiTietBCCongNo_DAO();
                return m_instance;
            }
        }


        /// <summary>
        ///   lay phieu ban hang co ngay be hon ngay bao cao
        ///  
        /// </summary>
        /// 

        public DataTable LayPhieuBanHang(DateTime _ngayLapBaoCao)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYPHIEUBANHANGTHEONGAY", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@ngayBan", SqlDbType.SmallDateTime);
                cmd.Parameters["@ngayBan"].Value = _ngayLapBaoCao.ToShortDateString();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   lay thong tin no moi nhat theo ma phieu ban hang
        /// </summary>

        public DataTable LayThongTinNo(string _id)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_PHIEUNO_CUOI_BY_PHIEUBANHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphieu", SqlDbType.NVarChar,10);
                cmd.Parameters["@maphieu"].Value = _id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   them thong tin ct bao cao
        /// </summary>
        /// 
        public bool ThemThongTin(string _maphieu, string _maphieuno, long _tienno)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_CTBAOCAOCONGNO", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaBaoCaoCongNo", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaBaoCaoCongNo"].Value = _maphieu;

                cmd.Parameters.Add("@MaPhieuNo", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaPhieuNo"].Value = _maphieuno;

                cmd.Parameters.Add("@TienNo", SqlDbType.Money);
                cmd.Parameters["@TienNo"].Value = (decimal)_tienno;

                cmd.ExecuteNonQuery();
                
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}
